﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Service.Interface;
using WCS_Service.Services;

namespace WCS.Controllers
{
    /// <summary>
    /// 自动化设备服务启动暂停接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AutoEquipmentOperateController : ControllerBase
    {
        private readonly IAutoEquipmentOperateService _autoEquipmentOperateService;

        public AutoEquipmentOperateController(IAutoEquipmentOperateService autoEquipmentOperateService,
                                              IConfiguration configuration)
        {
            _autoEquipmentOperateService = autoEquipmentOperateService;
        }

        #region 自动化设备服务管理
        /// <summary>
        /// 启动指定自动化设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> StartAutoEquipmentAsync([FromBody] int id)
        {
            var result = new WebApiCallBack();
            var info = await _autoEquipmentOperateService.CreatAutoEquipmentServiceAsync(id);

            result.status = info.HasValue ? info.Value : false;
            result.msg = info.HasValue ? info.Value ? "自动化设备服务启动成功" : "自动化设备服务启动失败"
                                        : "自动化设备服务已经启动";
            return result;
        }
        /// <summary>
        /// 启动批量自动化设备
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> StartBatchAutoEquipmentServiceAsync([FromBody] List<int> ids)
        {
            var result = new WebApiCallBack();
            var infos = await _autoEquipmentOperateService.CreatBatchAutoEquipmentServiceAsync(ids);
            var failFlag = infos.Values?.FirstOrDefault(v => !v);
            result.status = failFlag is null ? true: false;
            result.msg = result.status ? "批量自动化设备服务启动成功" : "批量自动化设备服务启动失败";
            return result;
        }
        /// <summary>
        /// 停止启动的自动化设备服务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> StopAutoEquipmentAsync([FromBody] int id)
        {
            var result = new WebApiCallBack();
            result.status = await _autoEquipmentOperateService.StopAutoEquipmentServiceAsync(id);
            result.msg = result.status ? "自动化服务停止成功" : "自动化服务停止失败";
            return result;
        }
        /// <summary>
        /// 停止批量自动化设备服务
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> StopBatchAutoEquipmentServiceAsync([FromBody] List<int> ids)
        {
            var result = new WebApiCallBack();
            result.status = await _autoEquipmentOperateService.StopBatchAutoEquipmentServiceAsync(ids);
            result.msg = result.status ? "批量自动化服务停止成功" : "批量自动化服务停止失败";
            return result;
        }
        /// <summary>
        /// 获取运行中的自动化设备
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> GetRunAutoEquipmentServiceAsync()
        {
            var result = new WebApiCallBack();
            var info = await _autoEquipmentOperateService.GetRunningAutoEquipmenServiceAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = "运行中设备为空！";
            }
            else
            {
                result.data = info;
            }
           return result;
        }
        /// <summary>
        /// 查询所有AGV运行数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetAllAgvRunInfoAsync()
        {
            var result = new WebApiCallBack();
            var info = await _autoEquipmentOperateService.GetAgvInfoAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = "运行中AGV为空！";
            }
            else
            {
                result.data = info;
            }
            return result;
        }
        #endregion
    }
}
